<template>
  <div class="user-management">
    <el-card class="management-card">
      <template #header>
        <div class="card-header">
          <span>用户管理</span>
          <el-button type="primary" @click="showAddDialog = true">
            <el-icon><Plus /></el-icon>
            添加用户
          </el-button>
        </div>
      </template>

      <!-- 用户列表 -->
      <el-table :data="userStore.users" v-loading="userStore.loading" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column prop="age" label="年龄" width="80" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <el-button size="small" @click="editUser(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="deleteUser(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑用户对话框 -->
    <el-dialog
      v-model="showAddDialog"
      :title="editingUser ? '编辑用户' : '添加用户'"
      width="500px"
    >
      <el-form :model="userForm" label-width="80px">
        <el-form-item label="姓名">
          <el-input v-model="userForm.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="userForm.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input-number v-model="userForm.age" :min="0" :max="150" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="showAddDialog = false">取消</el-button>
        <el-button type="primary" @click="saveUser" :loading="userStore.saving">
          {{ editingUser ? '更新' : '保存' }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import type { User, CreateUserRequest } from '../types/user'
import { useUserStore } from '../stores/userStore'

// 使用 Pinia store
const userStore = useUserStore()

// 响应式数据
const showAddDialog = ref(false)
const editingUser = ref<User | null>(null)

// 用户表单
const userForm = reactive({
  name: '',
  email: '',
  age: 18
})

// 编辑用户
const editUser = (user: User) => {
  editingUser.value = user
  Object.assign(userForm, user)
  showAddDialog.value = true
}

// 保存用户
const saveUser = async () => {
  try {
    if (editingUser.value) {
      await userStore.updateUser(editingUser.value.id, userForm)
    } else {
      await userStore.createUser(userForm as CreateUserRequest)
    }
    showAddDialog.value = false
    resetForm()
  } catch (error) {
    // 错误已在 store 中处理
  }
}

// 删除用户
const deleteUser = async (id: number) => {
  try {
    await userStore.deleteUser(id)
  } catch (error) {
    // 用户取消删除或删除失败
  }
}

// 重置表单
const resetForm = () => {
  editingUser.value = null
  userForm.name = ''
  userForm.email = ''
  userForm.age = 18
}

onMounted(() => {
  userStore.loadUsers()
})
</script>

<style scoped>
.user-management {
  max-width: 1200px;
  margin: 0 auto;
}

.management-card {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
